package leetcode

/*
 * @lc app=leetcode.cn id=123 lang=golang
 *
 * [123] 买卖股票的最佳时机 III
 */

// @lc code=start
func maxProfit3(prices []int) int {
	n := len(prices)
	if n < 2 {
		return 0
	}

	b1, s1, b2, s2 := -prices[0], 0, -prices[0], 0
	for i := 1; i < n; i++ {
		b1 = max(b1, -prices[i])
		s1 = max(s1, b1+prices[i])
		b2 = max(b2, s1-prices[i])
		s2 = max(s2, b2+prices[i])
	}
	return s2
}



// @lc code=end
